package com.small.site.controller;

import com.small.site.aspect.annotation.Log;
import com.small.site.pojo.SysRole;
import com.small.site.service.RoleService;
import com.small.site.util.R;
import com.small.site.validation.ValidationType;
import com.small.site.vo.PageVo;
import com.small.site.vo.req.RolePageReqVo;
import com.small.site.vo.req.RoleReqVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.Set;

/**
 * @author xiaochi
 * @date 2021/12/16 14:59
 * @desc RoleController
 */
@RestController
@RequestMapping("/manager")
public class RoleController extends BaseController{

    @Autowired
    private RoleService roleService;

    @PreAuthorize("hasAuthority('sys:role:list')")
    @Log(title = "组织管理-角色管理",action = "分页获取角色数据接口")
    @PostMapping("/roles/v1")
    public R<PageVo<SysRole>> pageInfo(@RequestBody RolePageReqVo rolePageReqVo){
        return R.ok(roleService.findPage(rolePageReqVo));
    }

    @PreAuthorize("hasAuthority('sys:role:add')")
    @Log(title = "组织管理-角色管理",action = "新增角色接口")
    @PostMapping("/role/v1")
    public R<String> createRole(@RequestBody @Valid RoleReqVo roleReqVo){
        return roleService.createRole(roleReqVo);
    }

    @PreAuthorize("hasAuthority('sys:role:detail')")
    @Log(title = "组织管理-角色管理",action = "获取角色详情接口")
    @GetMapping("/role/v1/{id}")
    public R<Set<String>> getRoleBy(@PathVariable("id") String roleId){
        return R.ok(roleService.getRoleById(roleId));
    }

    @PreAuthorize("hasAuthority('sys:role:update')")
    @Log(title = "组织管理-角色管理",action = "更新角色信息接口")
    @PutMapping("/role/v1")
    public R<String> updateRole(@RequestBody @Validated(ValidationType.update.class) RoleReqVo roleUpdateReqVo){
        return roleService.updateRole(roleUpdateReqVo);
    }

    @PreAuthorize("hasAuthority('sys:role:update')")
    @Log(title = "组织管理-角色管理",action = "更新角色状态接口")
    @PostMapping("/role/v1/{id}/{status}")
    public R<String> updateRoleStatus(@PathVariable("id")String roleId,@PathVariable("status") Integer status){
        return roleService.updateRoleStatus(roleId,status);
    }

    @PreAuthorize("hasAuthority('sys:role:delete')")
    @Log(title = "组织管理-角色管理",action = "删除角色接口")
    @DeleteMapping("/role/v1/{id}")
    public R<String> deletedRole(@PathVariable("id") String roleId){
        return roleService.deletedRole(roleId);
    }
}
